package com.example.demo.controller;

import com.example.demo.common.utills.CommonResult;
import com.example.demo.common.utills.FileUtil;
import com.example.demo.mapper.DataReportsMapper;
import com.example.demo.service.DataReportsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

@RestController
public class DataReportsController {

    @Resource
    DataReportsService dataReportsService;

    @Resource
    DataReportsMapper dataReportsMapper;

    @Resource
    HttpServletResponse httpServletResponse;


    @RequestMapping("/data_table")
    public CommonResult data_table(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize,
                                   @RequestParam(value = "name",required = false,defaultValue = "")String  name){
        return dataReportsService.selectAll(pageNum, pageSize, name);

    }

    @RequestMapping("/DFileLoadDown")
    public CommonResult fileDown(int id) throws FileNotFoundException, UnsupportedEncodingException {
        String path = dataReportsMapper.queryPath(id);
        FileUtil.downloadLocal(httpServletResponse,path);
        return new CommonResult(200,"下载成功");
    }

    @RequestMapping("/deleteDa")
    public CommonResult delete(int data_id){
        String path = dataReportsMapper.queryPath(data_id);
        String s = FileUtil.deleteFile(path);
        if(s.equals("success")){
            dataReportsMapper.delete(data_id);
            return new CommonResult(200,"删除成功");
        }
        return new CommonResult(500,"删除失败");
    }
}
